<?php

if( $_SERVER['REQUEST_METHOD'] == 'POST' ):
	session_start();

	$result = -1; // Retorna Erro para Flash.

	$user = isset($_POST['inputUsuario']) ? $_POST['inputUsuario'] : false;
	$pass = isset($_POST['inputSenha'])   ? $_POST['inputSenha']   : false;
	$secr = isset($_POST['SecurCuboWeb']) ? $_POST['SecurCuboWeb'] : false;
	$logout = isset($_POST['logout']) ? $_POST['logout'] : false;
	
	if( $user && $pass && $secr == "CuboWeb].'.[Desenvolver_09" ){
		require('funcoes.inc.php');
		$link = conecta();

		$user = str_replace("'","",$user);
		$pass = str_replace("'","",$pass);
		
		$sql = "SELECT users_id,users FROM users WHERE users='".$user."' && senha='".$pass."' && grupo='admin'";
		$query = mysql_query($sql,$link);
		$num   = mysql_num_rows($query);
		if( $num > 0 ){
			$result = mysql_result($query,0,1);
			$_SESSION['users_id'] = mysql_result($query,0,0);
			$_SESSION['users'] = $result;
			$_SESSION['group'] = 'admin';
		}
	}
	else	if( $logout == 'true' ){
				session_destroy();
				header("Location: ../../../index.php");		// rever
			}
	else	if( isset($_SESSION['users_id']) && $secr == "CuboWeb].'.[Desenvolver_09" )
				if( is_numeric($_SESSION['users_id']) )
					$result = $_SESSION['users'];
	
	echo 'answer='.$result;
	
else:

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CuboWeb - Login</title>

<script type="text/javascript">
function doMessage(message){
	alert(message);
}
function doRedirect(){
	window.top.location = '../../../index.php';
}
</script>
<script type="text/javascript" src="../js/include/swfobject.js"></script>

</head>

<body>
<script type="text/javascript" src="../js/modulos/modulo_login.js"></script>
</body>
</html>


<?php 
endif; ?>